package com.kizuki.json;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.junit.Test;

import java.util.*;

public class TestGson {

    private static List<People> list = new ArrayList<>();
    private static Map<String, People> map = new HashMap<>();

    private People p3 = new People(13, "李白2", 100, new Date());

    static {
        People p1 = new People(10, "李白", 100, new Date());
        People p2 = new People(11, "李黑", 200, new Date());
        list.add(p1);
        list.add(p2);
        map.put("李白", p1);
        map.put("李黑", p2);
    }

    @Test
    public void testObject() {
        Gson gson = new Gson();
        String jsonStr = gson.toJson(p3);
        System.out.println("jsonStr: " + jsonStr);
        People people = gson.fromJson(jsonStr, People.class);
        System.out.println("people: " + people);
    }

    @Test
    public void testList() {
        Gson gson = new Gson();
        String jsonStr = gson.toJson(list);
        System.out.println("jsonStr: " + jsonStr);
        List<People> list1 = gson.fromJson(jsonStr, new TypeToken<List<People>>(){}.getType());
        System.out.println("list1: " + list1);
    }

    @Test
    public void testMap() {
        Gson gson = new Gson();
        String jsonStr = gson.toJson(map);
        System.out.println("jsonStr: " + jsonStr);
        Map<String, People> map1 = gson.fromJson(jsonStr, new TypeToken<Map<String, People>>(){}.getType());
        System.out.println("map1: " + map1);
    }

    @Test
    public void testMan() {
        Gson gson = new Gson();
        String jsonStr = gson.toJson(new Man<>("Man"));
        System.out.println("jsonStr: " + jsonStr);
        Man<String> man = gson.fromJson(jsonStr, new TypeToken<Man<String>>(){}.getType());
        System.out.println("man: " + man);
    }

    @Data
    @AllArgsConstructor
    class Man<T>{
        private T data;
    }

}
